package zerous.infinite.core;

import com.badlogic.gdx.graphics.g2d.*;
import zerous.infinite.core.entity.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.*;
import zerous.infinite.util.*;
import com.badlogic.gdx.utils.*;
import java.util.*;
import com.badlogic.gdx.math.*;

public class GameWorld
{
	Sprite background;
	
	private OrthographicCamera camera;
	private Player player;
	private Batch batch;
	
	private Array<Entity> entities;
	
	public GameWorld() {
		player = new Player();
		camera = new OrthographicCamera(player.texture.getWidth()*16, player.texture.getHeight()*9);
		camera.position.set(player.position.x, player.position.y, 0);
		batch = new SpriteBatch();
		entities = new Array<Entity>();
		entities.add(new Enemy(player, 20, 20));
		background = new Sprite(new Texture(Gdx.files.internal("android.jpg")));
		background.setSize(background.getWidth()*10, background.getHeight()*10);
	}
	
	public Player getPlayer() {
		return player;
	}
	
	public Camera getCamera() {
		return camera;
	}
	
	public void update(float deltaTime) {
		int d = (int)MathUtil.distance(camera.position.x, camera.position.y, player.position.x, player.position.y);
		if (d >= 1) {
			float cos = (player.position.x - camera.position.x)/d;
			float sin = (player.position.y - camera.position.y)/d;
			camera.position.add(d * 0.05f * cos, d * 0.05f * sin, 0);
		}
		
		
	}
	
	public void draw() {
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		background.draw(batch);
		for (Entity entity : entities) {
			entity.draw(batch);
		}
		player.draw(batch);	
		batch.end();
		
		
		camera.update();
	}
}
